﻿// -----------------------------------------------------------------------
// <copyright file="VersionConfig.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//    GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
// -----------------------------------------------------------------------
#region Usings
using System;
using System.ComponentModel;
using System.IO;
using System.Xml.Schema;
using System.Xml.Serialization;
#endregion

namespace GateShift.Tools.DatabaseMigration.Configuration
{
    /// <summary>
    /// Version Configuration class for managing database updates for the server
    /// </summary>
    [XmlRoot(ElementName = "configuration", IsNullable = false)]
    [Serializable]
    public class VersionConfig
    {
        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the VersionConfig class
        /// </summary>
        public VersionConfig()
        {
            Versions = new DBVersionCollection();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a list of service files to use
        /// </summary>
        [XmlElement(Type = typeof(Version), ElementName = "version", IsNullable = false, Form = XmlSchemaForm.Qualified)]
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public DBVersionCollection Versions { get; set; }

        #endregion

        #region Loading/Saving

        /// <summary>
        /// Loads a VersionConfig from the specified file
        /// </summary>
        /// <param name="file">File to load the config from</param>
        /// <returns>VersionConfig with the XML deserialized classes</returns>
        public static VersionConfig Load(string file)
        {
            XmlSerializer s = new XmlSerializer(typeof(VersionConfig));

            using (FileStream fileStream = new FileStream(file, FileMode.Open))
            {
                return (VersionConfig)s.Deserialize(fileStream);
            }
        }

        /// <summary>
        /// Saves a VersionConfig to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        /// <param name="config">VersionConfig to save</param>
        public static void Save(string file, VersionConfig config)
        {
            XmlSerializer s = new XmlSerializer(typeof(VersionConfig));

            using (FileStream fileStream = new FileStream(file, FileMode.Open))
            {
                s.Serialize(fileStream, config);
            }
        }

        /// <summary>
        /// Saves a VersionConfig to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        public void Save(string file)
        {
            Save(file, this);
        }

        #endregion
    }
}
